package Stack;

import java.util.Arrays;
import java.util.EmptyStackException;

public class Stack {
    public int[] elem;
    public int usedSize;
    public int default_capacity=5;

    public Stack() {
        this.elem=new int[default_capacity];
    }
    public void push(int val) {     //压栈（入栈）
        if(isFull()) {
            resize();
        }
        elem[usedSize++]=val;
    }
    private boolean isFull() {
        return usedSize==elem.length;
    }
    private void resize() {
        elem= Arrays.copyOf(elem,elem.length*2);
    }
    public int pop() {    //出栈
        if(isEmpty()) {
            throw new EmptyStackException();
        }
        return elem[--usedSize];
    }
    private boolean isEmpty() {
        return usedSize==0;
    }
    public int size() {
        return usedSize;
    }
    public int peek() {
        if(isEmpty()) {
            throw new EmptyStackException();
        }
        return elem[usedSize-1];
    }
}
